﻿// *----------------------------------------------------------------
// Copyright (C) 2017 通通优品
// 版权所有。
// 
// 类名：IDatabase.cs
// 功能描述：TongTongMall.Dapper
// 
// Create User：jym 2017/02/09
// 
// Edit User：UserName,EditTime
// Describe：Describe
// ----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using TongTongMall.Dapper.DapperExtensions.Mapper;
using TongTongMall.Dependecy;
using TongTongMall.Domain.PredicateForDapper;

namespace TongTongMall.Dapper.DapperExtensions
{
    public interface IDatabase : ITransientDependency
    {
        /// <summary>
        /// 是否存在活跃的事务
        /// </summary>
        bool HasActiveTransaction { get; }

        /// <summary>
        /// 获取connection实例
        /// </summary>
        IDbConnection Connection { get; }

        /// <summary>
        /// 插入一个实体，包含事务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="transaction"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        dynamic Insert<T>(T entity, IDbTransaction transaction, int? commandTimeout = null) where T : class,new();

        /// <summary>
        /// 异步插入一个实体，包含事务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="transaction"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        Task<dynamic> InsertAsync<T>(T entity, IDbTransaction transaction, int? commandTimeout = null) where T : class,new();


        /// <summary>
        /// 插入一个实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        dynamic Insert<T>(T entity, int? commandTimeout = null) where T : class,new();

        /// <summary>
        /// 异步插入一个实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        Task<dynamic> InsertAsync<T>(T entity, int? commandTimeout = null) where T : class,new();
        /// <summary>
        /// 更新一个实体，包含事务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="transaction"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        bool Update<T>(T entity, IDbTransaction transaction, int? commandTimeout = null) where T : class, new();

        /// <summary>
        /// 异步更新一个实体，，包含事务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="transaction"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        Task<bool> UpdateAsync<T>(T entity, IDbTransaction transaction, int? commandTimeout = null) where T : class, new();
        /// <summary>
        /// 更新实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        bool Update<T>(T entity, int? commandTimeout = null) where T : class, new();

        /// <summary>
        /// 更新实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        Task<bool> UpdateAsync<T>(T entity, int? commandTimeout = null) where T : class, new();
        /// <summary>
        /// 更新指定字段，包含事务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="id"></param>
        /// <param name="transaction"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        bool Update<T>(object entity, object id, IDbTransaction transaction, int? commandTimeout = null) where T : class,new();

        /// <summary>
        /// 异步更新指定字段，包含事务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="id"></param>
        /// <param name="transaction"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        Task<bool> UpdateAsync<T>(object entity, object id, IDbTransaction transaction, int? commandTimeout = null) where T : class,new();

        /// <summary>
        /// 更新指定字段
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="whereClip"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        bool Update<T>(object entity, object whereClip, int? commandTimeout = null) where T : class,new();

        /// <summary>
        /// 异步更新指定字段
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="whereClip"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        Task<bool> UpdateAsync<T>(object entity, object whereClip, int? commandTimeout = null) where T : class,new();

    
        /// <summary>
        /// 根据删除条件删除对象，包含事务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="transaction"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        bool Delete<T>(object predicate, IDbTransaction transaction, int? commandTimeout = null) where T : class,new();

        /// <summary>
        /// 异步根据删除条件删除对象，包含事务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="transaction"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        Task<bool> DeleteAsync<T>(object predicate, IDbTransaction transaction, int? commandTimeout = null) where T : class,new();

        /// <summary>
        /// 根据删除条件删除对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        bool Delete<T>(object predicate, int? commandTimeout = null) where T : class,new();

        /// <summary>
        /// 异步根据删除条件删除对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        Task<bool> DeleteAsync<T>(object predicate, int? commandTimeout = null) where T : class,new();

        /// <summary>
        /// 根据条件获取列表，包含事务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="sort"></param>
        /// <param name="transaction"></param>
        /// <param name="commandTimeout"></param>
        /// <param name="buffered"></param>
        /// <returns></returns>
        IEnumerable<T> GetList<T>(object predicate, IList<ISort> sort, IDbTransaction transaction, Expression<Func<T, object>> selector = null, int? commandTimeout = null, bool buffered = true) where T : class,new();

        /// <summary>
        /// 异步根据条件获取列表，包含事务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="sort"></param>
        /// <param name="transaction"></param>
        /// <param name="commandTimeout"></param>
        /// <param name="buffered"></param>
        /// <returns></returns>
        Task<IEnumerable<T>> GetListAsync<T>(object predicate, IList<ISort> sort, IDbTransaction transaction, Expression<Func<T, object>> selector = null, int? commandTimeout = null, bool buffered = true) where T : class,new();

        /// <summary>
        /// 根据条件获取列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="sort"></param>
        /// <param name="commandTimeout"></param>
        /// <param name="buffered"></param>
        /// <returns></returns>
        IEnumerable<T> GetList<T>(object predicate = null, IList<ISort> sort = null, Expression<Func<T, object>> selector = null, int? commandTimeout = null, bool buffered = true) where T : class,new();

        /// <summary>
        /// 异步根据条件获取列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="sort"></param>
        /// <param name="commandTimeout"></param>
        /// <param name="buffered"></param>
        /// <returns></returns>
        Task<IEnumerable<T>> GetListAsync<T>(object predicate = null, IList<ISort> sort = null, Expression<Func<T, object>> selector = null, int? commandTimeout = null, bool buffered = true) where T : class,new();

        /// <summary>
        /// 根据条件获取行数，包含事务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="transaction"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        int Count<T>(object predicate, IDbTransaction transaction, int? commandTimeout = null) where T : class,new();

        /// <summary>
        /// 异步根据条件获取行数，包含事务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="transaction"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        Task<int> CountAsync<T>(object predicate, IDbTransaction transaction, int? commandTimeout = null) where T : class,new();

        /// <summary>
        /// 根据条件获取行数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        int Count<T>(object predicate, int? commandTimeout = null) where T : class,new();

        /// <summary>
        /// 异步根据条件获取行数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        Task<int> CountAsync<T>(object predicate, int? commandTimeout = null) where T : class,new();

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="whereClip"></param>
        /// <param name="orderClip"></param>
        /// <param name="selector"></param>
        /// <returns></returns>
        IEnumerable<T> QueryPaged<T>(int pageIndex, int pageSize, object whereClip = null, IList<ISort> orderClip = null, Expression<Func<T, object>> selector = null, int? commandTimeout = default(int?), bool buffered = true) where T : class, new();

        /// <summary>
        /// 异步获取分页数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="whereClip"></param>
        /// <param name="orderClip"></param>
        /// <param name="selector"></param>
        /// <returns></returns>
        Task<IEnumerable<T>> QueryPagedAsync<T>(int pageIndex, int pageSize, object whereClip = null, IList<ISort> orderClip = null, Expression<Func<T, object>> selector = null, int? commandTimeout = default(int?)) where T : class, new();

        /// <summary>
        /// 清空缓存
        /// </summary>
        void ClearCache();

        /// <summary>
        /// 获取entity对应的配置信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        IClassMapper GetMap<T>() where T : class,new();

        /// <summary>
        /// 设置数据库连接
        /// </summary>
        /// <param name="DbConnection"></param>
        void SetConnection(IDbConnection DbConnection);
    }
}
